home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / components / example / DialogWindow.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  107 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. public class DialogWindow extends Frame {
  20.     private boolean inAnApplet = true;
  21.     private SimpleDialog dialog;
  22.     private TextArea textArea;
  23.  
  24.     public DialogWindow() {
  25.         textArea = new TextArea(5, 40);
  26.         textArea.setEditable(false);
  27.         add("Center", textArea);
  28.         Button button = new Button("Click to bring up dialog");
  29.         Panel panel = new Panel();
  30.         panel.add(button);
  31.         add("South", panel);
  32.     }
  33.  
  34.     public boolean handleEvent(Event event) {
  35.         if (event.id == Event.WINDOW_DESTROY) {
  36.             if (inAnApplet) {
  37.                 dispose();
  38.             } else {
  39.                 System.exit(0);
  40.             }
  41.         }   
  42.         return super.handleEvent(event);
  43.     }
  44.  
  45.     public boolean action(Event event, Object arg) {
  46.         if (dialog == null) {
  47.             dialog = new SimpleDialog(this, "A Simple Dialog");
  48.         }
  49.         dialog.show();
  50.         return true;
  51.     }
  52.  
  53.     public void setText(String text) {
  54.         textArea.appendText(text + "\n");
  55.     }
  56.  
  57.     public static void main(String args[]) {
  58.         DialogWindow window = new DialogWindow();
  59.         window.inAnApplet = false;
  60.  
  61.         window.setTitle("DialogWindow Application");
  62.         window.pack();
  63.         window.show();
  64.     }
  65. }
  66.  
  67. class SimpleDialog extends Dialog {
  68.     TextField field;
  69.     DialogWindow parent;
  70.     Button setButton;
  71.  
  72.     SimpleDialog(Frame dw, String title) {
  73.         super(dw, title, false);
  74.         parent = (DialogWindow)dw;
  75.  
  76.         //Create middle section.
  77.     Panel p1 = new Panel();
  78.         Label label = new Label("Enter random text here:");
  79.         p1.add(label);
  80.         field = new TextField(40);
  81.     p1.add(field);
  82.         add("Center", p1);
  83.  
  84.         //Create bottom row.
  85.         Panel p2 = new Panel();
  86.         p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
  87.         Button b = new Button("Cancel");
  88.         setButton = new Button("Set");
  89.         p2.add(b);
  90.         p2.add(setButton);
  91.         add("South", p2);
  92.  
  93.     //Initialize this dialog to its preferred size.
  94.     pack();
  95.     }
  96.  
  97.     public boolean action(Event event, Object arg) {
  98.         if ( (event.target == setButton)
  99.            | (event.target == field)) {
  100.             parent.setText(field.getText());
  101.         }
  102.         field.selectAll();
  103.         hide();
  104.         return true;
  105.     }
  106. }
  107.